package com.yonyou.pmclouds.repeatwork.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.IllegalRequestArgException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.repeatwork.entity.RepeatWorkVO;
import com.yonyou.pmclouds.repeatwork.service.rmiitf.IRepeatWorkMaintain;
import com.yonyou.pmclouds.repeatwork.service.rmiitf.IRepeatWorkQuery;
import com.yonyou.pmclouds.safetyrectification.entity.SafetyRectificationVO;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IWorkFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 复工单
 */
@RestController
@RequestMapping({"mobile/repeatWork","repeatWork"})
public class RepeatWorkController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IRepeatWorkMaintain repeatWorkMaintain;
    @Reference(version=ApplicationConsts.APPLICATION_VERSION)
    private IRepeatWorkQuery repeatWorkQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IWorkFlowService iworkFlowService;
    @Autowired
    private UserInfoQuery userQuery;

    private static final String[] USER_ID_FIELDS = {RepeatWorkVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {RepeatWorkVO.CREATOR_NAME};

    private static final String[] COPYPERSON_ID_FIELDS = {"copyPerson"};
    private static final String[] COPYPERSON_NAME_FIELDS = {"copyPersonName"};
    /**
     * 保存复工单
     * @param vo
     * @param errors
     * @return
     * @throws BusinessException
     */
    @Insert
    @PostMapping("/add")
    @PMApprove
    public RepeatWorkVO add(@RequestBody @Validated RepeatWorkVO vo, BindingResult errors) throws BusinessException {
        if (errors.hasErrors())
            throw new IllegalRequestArgException();
        initInsertVO(vo);
        //智能日志需要制单人信息
        String creatorName = userQuery.queryUserName(RuntimeEnvironment.getLoginUserId());
        vo.setCreatorName(creatorName);
        RepeatWorkVO resultVO = repeatWorkMaintain.add(vo);
        //翻译抄送入姓名
        transCopyPersonName(resultVO);
        return resultVO;
    }

    /**
     * 修改复工单
     * @param vo
     * @param errors
     * @return
     * @throws BusinessException
     */
    @PostMapping("/update")
    @PMApprove(trigger = "commit")
    public RepeatWorkVO update(@RequestBody @Validated RepeatWorkVO vo, BindingResult errors) throws  BusinessException{
        if (errors.hasErrors())
            throw new IllegalRequestArgException();

        RepeatWorkVO resultVO = repeatWorkMaintain.update(vo);

        return resultVO;
    }

    /**
     * 查看复工单详细信息
     * @param pkRepeatWork
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/getRepeatWorkDetail")
    public RepeatWorkVO getRepeatWorkDetail(String pkRepeatWork) throws BusinessException{
        RepeatWorkVO repeatWorkVO = repeatWorkQuery.getReport(pkRepeatWork);

        //翻译抄送人名字
        transCopyPersonName(repeatWorkVO);

        //追加创建者姓名
        userQuery.tranUserInfoField(new RepeatWorkVO[]{repeatWorkVO}, USER_ID_FIELDS, USER_NAME_FIELDS);

        return repeatWorkVO;
    }

    /**
     * 初始化单据VO里的单据类型和单据状态
     * @param vo
     */
    private void initInsertVO(RepeatWorkVO vo) {
        if (vo == null) return;
        vo.setBillType(BillTypeConst.REPEATWORK_BILLTYPE);
        vo.setBillstatus(BillStatusConst.COMMON_FREE_STATUS);
    }

    /**
     * 翻译复工单里的抄送人
     * @param vo
     */
    private void transCopyPersonName(RepeatWorkVO vo){
        List<CopyPersonVO> copyPersonVOList = vo.getCopyPersons();
        if(null!=copyPersonVOList  && copyPersonVOList.size() > 0 ){
            //翻译抄送人名称
            userQuery.tranUserInfoField(copyPersonVOList.toArray(new CopyPersonVO[0]), COPYPERSON_ID_FIELDS, COPYPERSON_NAME_FIELDS);
        }
    }
}
